﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Globalization;

namespace TPG.SharePoint.Workflow
{
    /// <summary>
    /// Displays a list of tasks that can be operated on in bulk.
    /// </summary>
    public class BulkTaskList : Page
    {
        /// <summary>
        /// 
        /// </summary>
        protected Repeater Associations;

        protected override void OnLoad(EventArgs e)
        {
            SPList list = SPContext.Current.List;

            Dictionary<SPWorkflowAssociation, string[]> workflowAssociations = new Dictionary<SPWorkflowAssociation, string[]>();

            foreach (SPContentType contentType in list.ContentTypes)
                foreach (SPWorkflowAssociation wfassoc in contentType.WorkflowAssociations)
                    if (!workflowAssociations.ContainsKey(wfassoc) && (wfassoc.BaseTemplate["Task_BulkIds"] != null))
                        workflowAssociations.Add(wfassoc, null);
            foreach (SPWorkflowAssociation wfassoc in list.WorkflowAssociations)
                if (!workflowAssociations.ContainsKey(wfassoc) && (wfassoc.BaseTemplate["Task_BulkIds"] != null))
                    workflowAssociations.Add(wfassoc, null);

            Associations.DataSource = workflowAssociations.Keys;
            Associations.DataBind();

            base.OnLoad(e);
        }

        protected string FormatUrl(Guid assocId, int taskType)
        {
            string url = null;
            SPUtility.DetermineRedirectUrl("WSS3Workflow/BulkTaskFormHost.aspx",
                SPRedirectFlags.RelativeToLayoutsPage, this.Context,
                string.Format(CultureInfo.InvariantCulture, "List={0}&AssocID={1}&TaskType={2}", SPContext.Current.List.ID, assocId, taskType), out url);
            return url;
        }

        protected void Associations_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                SPWorkflowAssociation association = e.Item.DataItem as SPWorkflowAssociation;

                string[] taskIds = (association["Task_BulkIds"] as string).Split(',');

                List<BulkTaskDefinition> taskNames = new List<BulkTaskDefinition>();
                foreach (string taskIdString in taskIds)
                {
                  int taskId = Int32.Parse(taskIdString.Trim());
                  string taskName = WorkflowConfiguration.LookupMetadata(association.BaseTemplate,
                    PageType.Task, taskId.ToString(), "Name", WorkflowConfiguration.DataTypeText, false);
                  taskNames.Add(new BulkTaskDefinition(
                    taskName, taskId, association.Id));
                }
                Repeater tasks = e.Item.FindControl("Tasks") as Repeater;
                tasks.DataSource = taskNames;
                tasks.DataBind();
            }
        }
    }
}
